package model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import app.PMF;
import beans.BeneffitBean;
import beans.CarrierBean;
import beans.MestoBean;
import beans.TripPartBean;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class PutnaDuz implements Serializable {

	private static final long serialVersionUID = 3124815164976310031L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key sifra;

	@Persistent
	private double cena;
	@Persistent
	private Date polazak;
	@Persistent
	private Date dolazak;

	@Persistent
	public List<Key> povlastica;
	@Persistent
	public Key mestoOd;
	@Persistent
	public Key mestoDo;
	@Persistent
	public Key prevoznik;

	public PutnaDuz() {
		super();
	}

	public PutnaDuz(double cena, Date polazak, Date dolazak, List<Key> povlastica,
			Key mestoOd, Key mestoDo, Key prevoznik) {
		super();
		this.cena = cena;
		this.polazak = polazak;
		this.dolazak = dolazak;
		this.povlastica = povlastica;
		this.mestoOd = mestoOd;
		this.mestoDo = mestoDo;
		this.prevoznik = prevoznik;
	}

	public PutnaDuz(Key sifra, double cena, Date polazak, Date dolazak,
			List<Key> povlastica, Key mestoOd, Key mestoDo, Key prevoznik) {
		super();
		this.sifra = sifra;
		this.cena = cena;
		this.polazak = polazak;
		this.dolazak = dolazak;
		this.povlastica = povlastica;
		this.mestoOd = mestoOd;
		this.mestoDo = mestoDo;
		this.prevoznik = prevoznik;
	}

	public Key getSifra() {
		return sifra;
	}

	public void setSifra(Key sifra) {
		this.sifra = sifra;
	}

	public double getCena() {
		return cena;
	}

	public void setCena(double cena) {
		this.cena = cena;
	}

	public List<Povlastica> getPovlastica() {
		// ova metoda ima prostora za optimizaciju
		if (povlastica == null) {
			return null;
		}

		List<Povlastica> retVal = new ArrayList<Povlastica>();

		for (Key key : povlastica) {
			Povlastica povl = Util.getEntityFromTableByID(Povlastica.class,
					key.getId() + "");
			if (povl != null)
				retVal.add(povl);
		}
		return retVal;
	}

	public void setPovlastica(List<Key> povlastica) {
		this.povlastica = povlastica;
	}

	public Mesto getMestoOd() {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Mesto mesto = (Mesto) pm.getObjectById(Mesto.class, mestoOd);
		pm.close();

		return mesto;
	}

	public void setMestoOd(Key mestoOd) {
		this.mestoOd = mestoOd;
	}

	public Mesto getMestoDo() {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Mesto mesto = (Mesto) pm.getObjectById(Mesto.class, mestoDo);
		pm.close();

		return mesto;
	}

	public void setMestoDo(Key mestoDo) {
		this.mestoDo = mestoDo;
	}

	public Prevoznik getPrevoznik() {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Prevoznik p = (Prevoznik) pm.getObjectById(Prevoznik.class, prevoznik);
		pm.close();

		return p;
	}

	public boolean uporediPrevoznika(Key prev) {
		return prevoznik.equals(prev);
	}

	public void setPrevoznik(Key prevoznik) {
		this.prevoznik = prevoznik;
	}

	public Date getPolazak() {
		return polazak;
	}

	public void setPolazak(Date polazak) {
		this.polazak = polazak;
	}

	public Date getDolazak() {
		return dolazak;
	}

	public void setDolazak(Date dolazak) {
		this.dolazak = dolazak;
	}
	
	public TripPartBean toTripPartBean(){
		ArrayList<BeneffitBean> bens = new ArrayList<BeneffitBean>();

		for(Key key : povlastica){
			BeneffitBean ben = Util.getEntityFromTableByID(Povlastica.class, key.getId()+"").toBeneffitBean();
			bens.add(ben);
		}

		MestoBean mOd = Util.getEntityFromTableByID(Mesto.class, mestoOd.getId()+"").toBMestoBean();
		MestoBean mDo = Util.getEntityFromTableByID(Mesto.class, mestoDo.getId()+"").toBMestoBean();
		CarrierBean prev = Util.getEntityFromTableByID(Prevoznik.class, prevoznik.getId()+"").toCarrierBean();	

		
		return new TripPartBean(Integer.valueOf((int)sifra.getId()), Double.valueOf(cena), 
						polazak, dolazak, bens, mOd,mDo,prev );
	}

	public String toString() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

		String retVal = "PutnaDuz - ID: " + sifra.getId() + " , cena: " + cena;
		retVal += ", polazak: " + sdf.format(polazak);
		retVal += ", dolazak: " + sdf.format(dolazak);
		retVal += ", Ima povlastice: ";

		for(Povlastica povl : getPovlastica()){
			retVal += povl + " "; 
		}			   

		retVal += "OD " + mestoOd + "Do " + mestoDo + prevoznik;
		return retVal;
	}
}